#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/cdev.h>
#include <linux/of.h>
#include <linux/of_address.h>


#define LED_CNT 1
#define NAME "dtsled"

static void __iomem *data_reg;
static void __iomem *dirm_reg;
static void __iomem *outen_reg;
static void __iomem *intdis_reg;
static void __iomem *aper_clk_reg;

struct dtsled_dev{
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node *nd;
};

static struct dtsled_dev dtsled;


static int dtsled_open(struct inode *inode, struct file *filp)
{
    filp->private_data=&dtsled;

    printk("dtsled open!\n");
    return 0;
}

static int dtsled_release(struct inode *inode, struct file *filp)
{
    printk("dtsled released!\n");
    return 0;
}

static ssize_t dtsled_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t dtsled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int ret,val;
    char kern_buf[1];
    ret=copy_from_user(kern_buf,buf,cnt);

    val=readl(data_reg);
    if(kern_buf[0]==0){
        val &=~(1<<7);
    }
    else{
        val |=1<<7;
    }
    writel(val,data_reg);

    return 0;
}

static struct file_operations dtsled_fops={
    .owner=THIS_MODULE,
    .open=dtsled_open,
    .read=dtsled_read,
    .write=dtsled_write,
    .release=dtsled_release,
};

static void dtsled_ioremap(void)
{
    data_reg=of_iomap(dtsled.nd,0);
    dirm_reg=of_iomap(dtsled.nd,1);
    outen_reg=of_iomap(dtsled.nd,2);
    intdis_reg=of_iomap(dtsled.nd,3);
    aper_clk_reg=of_iomap(dtsled.nd,4);
}

static void dtsled_iounmap(void)
{
    iounmap(data_reg);
    iounmap(dirm_reg);
    iounmap(outen_reg);   
    iounmap(intdis_reg);
    iounmap(aper_clk_reg);     
}


static int __init dtsled_init(void)
{
    u32 val;
    int ret;
    const char *str;

    /*0*/
    dtsled.nd=of_find_node_by_path("/led");
    ret=of_property_read_string(dtsled.nd, "status", &str);
    if(strcmp(str, "okay")){
        return -EINVAL;
    }

    ret=of_property_read_string(dtsled.nd, "compatible", &str);
    if(strcmp(str, "alientek,led")){
        return -EINVAL;
    }

    printk(KERN_ERR "led device matching successful!\n");

    /*1*/
    dtsled_ioremap();

    /*2*/
    val=readl(aper_clk_reg);
    val|=1<<22;
    writel(val,aper_clk_reg);

    /*3*/
    val=0;
    val|=1<<7;
    writel(val,intdis_reg);

    /*4*/
    val=readl(dirm_reg);
    val |=1<<7;
    writel(val,dirm_reg);

    /*5*/
    val=readl(outen_reg);
    val|=1<<7;
    writel(val,outen_reg);

    /*6*/
    val=readl(data_reg);
    ret=of_property_read_string(dtsled.nd, "default-state", &str);
    if(!strcmp(str, "on")){
        val |=1<<7;
    }
    else {
        val &=~(1<<7);
    }
    writel(val,data_reg);

    /*7*/
    if(dtsled.major){
        dtsled.devid=MKDEV(dtsled.major,0);
        ret=register_chrdev_region(dtsled.devid, LED_CNT, NAME);
    }
    else {
        ret=alloc_chrdev_region(&dtsled.devid, 0, LED_CNT, NAME);
        dtsled.major=MAJOR(dtsled.devid);
        dtsled.minor=MINOR(dtsled.devid);
    }

    printk("dtsled major=%d, minor=%d\n", dtsled.major, dtsled.minor);

    /*8*/
    dtsled.cdev.owner=THIS_MODULE;
    cdev_init(&dtsled.cdev, &dtsled_fops);
    ret=cdev_add(&dtsled.cdev, dtsled.devid, LED_CNT);

    /*9*/
    dtsled.class=class_create(THIS_MODULE, NAME);
    dtsled.device=device_create(dtsled.class,
                                    NULL,
                                    dtsled.devid,
                                    NULL,
                                    NAME);
    
    return 0;
}

static void __exit dtsled_exit(void)
{
    device_destroy(dtsled.class, dtsled.devid);
    class_destroy(dtsled.class);
    cdev_del(&dtsled.cdev);
    unregister_chrdev_region(dtsled.devid, LED_CNT);
    dtsled_iounmap();

    printk("dtsled_exit()\n");
}

module_init(dtsled_init);
module_exit(dtsled_exit);

MODULE_AUTHOR("xujianchao");
MODULE_DESCRIPTION("xujianchao ZYNQ GPIO LED Driver");
MODULE_LICENSE("GPL");
